
var swakSampleMaxLen = 4;

var swakstats = {
	numOTSVotersAvg: 0,
	numOTSVotersStdev: 0,
	numOTSVotersSample: 0,
	OTSTrend: 0,
	numOTSContests: 0,
	numOTSCorrect: 0,
	numOTSPlayed: 0,
	numOTSWins1: 0,
	numOTSWins2: 0,
	numOTSWins3: 0,
	numOTSWins4: 0,
	numOTSWins5: 0,
	numOTSWins6: 0,
	incrementOutwitOptIn: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_OPTINCOUNT);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_OPTINCOUNT,count);
		}
		catch(x){}
	},
	incrementOutwitSelected: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_SELECTED);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_SELECTED,count);
		}
		catch(x){}
	},	
	incrementOutwitVote: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_VOTECOUNT);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_VOTECOUNT,count);
		}
		catch(x){}		
	},
	incrementOutwitPoint: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OW_POINTCOUNT);
			count++;
			ssopts.setIntPref(SMACK_STAT_OW_POINTCOUNT,count);
		}
		catch(x){}		
	},
	incrementSmackPotPlayed: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_SP_PLAYED);
			count++;
			ssopts.setIntPref(SMACK_STAT_SP_PLAYED,count);
		}
		catch(x){}		
	},
	incrementSmackPotBet: function(coins) {
		if (coins == null) coins = 0;
		try {
			var count = SS_getPreference(SMACK_STAT_SP_COINBETCOUNT);
			count = count+coins;
			ssopts.setIntPref(SMACK_STAT_SP_COINBETCOUNT,count);
		}
		catch(x){}		
	},
	incrementSmackPotCoinsWon: function(coins) {
		if (coins == null) coins = 0;
		try {
			var count = SS_getPreference(SMACK_STAT_SP_COINWONCOUNT);
			count = count+coins;
			ssopts.setIntPref(SMACK_STAT_SP_COINWONCOUNT,count);
		}
		catch(x){}		
	},
	incrementGuruGuessPlayed: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_GG_PLAYED);
			count++;
			ssopts.setIntPref(SMACK_STAT_GG_PLAYED,count);
		}
		catch(x){}		
	},
	incrementOTS1: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OTS_1WIN);
			count++;
			ssopts.setIntPref(SMACK_STAT_OTS_1WIN,count);
			swakstats.numOTSWins1++;
		}
		catch(x){}		
	},
	incrementOTS2: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OTS_2WIN);
			count++;
			ssopts.setIntPref(SMACK_STAT_OTS_2WIN,count);
			swakstats.numOTSWins2++;
		}
		catch(x){}		
	},
	incrementOTS3: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OTS_3WIN);
			count++;
			ssopts.setIntPref(SMACK_STAT_OTS_3WIN,count);
			swakstats.numOTSWins3++;
		}
		catch(x){}		
	},
	incrementOTS4: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OTS_4WIN);
			count++;
			ssopts.setIntPref(SMACK_STAT_OTS_4WIN,count);
			swakstats.numOTSWins4++;
		}
		catch(x){}		
	},
	incrementOTS5: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OTS_5WIN);
			count++;
			ssopts.setIntPref(SMACK_STAT_OTS_5WIN,count);
			swakstats.numOTSWins5++;
		}
		catch(x){}		
	},
	incrementOTS6: function() {
		try {
			var count = SS_getPreference(SMACK_STAT_OTS_6WIN);
			count++;
			ssopts.setIntPref(SMACK_STAT_OTS_6WIN,count);
			swakstats.numOTSWins6++;
		}
		catch(x){}		
	},
	incrementOTSWinner: function(winningPick) {
		try {
			switch(winningPick) {
				case 1:
					swakstats.incrementOTS1();
					break;
				case 2:
					swakstats.incrementOTS2();
					break;
				case 3:
					swakstats.incrementOTS3();
					break;
				case 4:
					swakstats.incrementOTS4();
					break;
				case 5:
					swakstats.incrementOTS5();
					break;
				case 6:
					swakstats.incrementOTS6();
					break;																									
			}

		}
		catch(x){}		
	}								
}

function swakBuildOTSResult() {
	var otsresult = null;
	try {
		otsresult = new SwakOTSResult();
		

		var results = swaksmack.SS_JF.getOTSResultsPercents();
		var ans = swaksmack.SS_JF.getOTSResults();
		//var nv = swaksmack.SS_JF.calcNumOTSPlayers(results);
		
		otsresult.numVoters = swaksmack.SS_JF.calcNumOTSPlayers(results);
		otsresult.answers = ans.slice();
		otsresult.contestant = swaksmack.SS_JF.getContestant();
		otsresult.contestantPage = swaksmack.SS_JF.getContestantPage();
		otsresult.contestantAnswerId = swaksmack.SS_JF.getContestantAnswerId();
		otsresult.oid = swaksmack.SS_JF.getLOID();
		otsresult.date = new Date();
		otsresult.potSize = swaksmack.SS_JF.getOutwitPot();
		otsresult.amountWon = swaksmack.SS_JF.getAmountWon();
		if (otsresult.amountWon > 0) {
			otsresult.winner = true;
		}
		otsresult.randomSuggestion = swakOTSRandomChoice;
		otsresult.contestantAvatar = swaksmack.SS_JF.getContestantAvatar();
		otsresult.question = swaksmack.SS_JF.getOTSQuestion();
		otsresult.winningAnswers =swaksmack.SS_JF.getOTSWinningAnswers();
//		otsresult.userChoice = swakOTSUserChoice;
		otsresult.winningAnswerValues = [];
		otsresult.answerPercents = [];

		//store the user choice (form is in the answer id, not an int)
		var uc = swaksmack.SS_JF.getOTSUserChoice();
		//fill in percents and txt answers
		swaksmack.SS_JF.buildOTSStatsAnswers(otsresult,uc);
		
		
		
	}
	catch (x) {
		swakAddException(x);
		otsresult = null;
	}
	
	return otsresult;
}

function SwakOTSResult() {
	//use liveobject id as oid
	this.oid = null;
	this.date = null;
	this.contestant = null;
	this.contestantAvatar = null;
	this.contestantPage = null;
	this.contestantAnswerId = null;
	this.contestantChoice = -1;
	this.winner = false;
	this.potSize = 0;
	this.amountWon = 0;
	this.question = null;
	this.winningAnswerStr = null;
	this.winningAnswerNum = -1;
	this.numVoters = 0;
	//array of answers
//	this.answers = null;
	this.winningAnswers = null;
	//array of picks
	this.winningAnswerValues = null;
	this.userChoice = -1;
	this.randomSuggestion = -1;
	this.answerPercents = null;
	//array of answer txt
	this.answers = null;

	this.innerHTML = null;
	
   	this.toString = function() { var str = 'Date: ' + this.date; str+='\n';
     	str+='OID: ' +this.oid; str+='\n'; str+='Question: ' +this.question;
      	str+='\n'; str+='Answer text: ' +this.winningAnswerStr; str+='\n';
	str+='Answer number: ' +this.winningAnswerNum; str+='\n'; str+='Contestant: '
           	+this.contestant; str+='\n'; str+='Contestant avatar: '
 	+this.contestantAvatar; str+='\n'; str+='Winner: ' +this.winner; str+='\n';
  	str+='Amount won: ' +this.amountWon; str+='\n'; str+='Number of voters: '
    	+this.numVoters; str+='\n'; str+='Answers: ' +this.answers; str+='\n';
         	str+='Winning Answers: ' +this.winningAnswers; return str; }
	
	
	this.addWinningAnswer = function(value) {
		if (this.winningAnswerValues == null) {
			this.winningAnswerValues = new Array();
		}
		this.winningAnswerValues.push(value);
	}
	
	this.buildDelimitedString = function() {
		var delimstr = '';
		try {
			var includeAnswers = SS_getPreference(SMACK_STAT_OTS_DELIMITED_CAP_TXTANS_ENABLED);
			if (this.winningAnswerValues == null) return '';
			var dd = this.date.formatDate("n/j/Y");
			var dt = this.date.formatDate("H:i:s");
			var epoch = this.date.formatDate("U");
			var len = this.winningAnswerValues.length;
			var otsanswlen = this.answers.length;
			var winmatrix = [0,0,0,0,0,0];

			
			var cAvatar = this.contestantAvatar;
			cAvatar = 'http://www.jellyfish.com' + cAvatar;
			var cPage = 'http://www.jellyfish.com/shoppers/' + this.contestantPage;
			//delimstr = delimstr + this.oid + ',' + epoch + ',' + dd + ',' + dt + ',"' + this.question + '","' + this.contestant+'",'+this.winner+','+this.amountWon+','+ this.numVoters + ',' + this.randomSuggestion + ',' + this.userChoice + ',' + this.answerPercents[0] + ',' + this.answerPercents[1] + ',' + this.answerPercents[2] + ','+ this.answerPercents[3] + ',' + this.answerPercents[4] + ','+ this.answerPercents[5];
			delimstr = delimstr + this.oid + ',' + epoch + ',' + dd + ',' + dt + ',"' + this.question + '"'; //,"' + this.contestant+'",'+this.winner+','+this.amountWon+','+ this.numVoters + ',' + this.randomSuggestion + ',' + this.userChoice + ',' + this.answerPercents[0] + ',' + this.answerPercents[1] + ',' + this.answerPercents[2] + ','+ this.answerPercents[3] + ',' + this.answerPercents[4] + ','+ this.answerPercents[5];
			if (includeAnswers == true) {
				for (var i=0;i<otsanswlen;i++) {
					delimstr = delimstr + ',"' + this.answers[i].text + '"';
				}
			}
			
			delimstr = delimstr + ',"' + this.contestant+'","'+cAvatar+'","'+cPage+'",'+this.contestantChoice+','+this.winner+',' + this.potSize + ',' +this.amountWon+','+ this.numVoters + ',' + this.randomSuggestion + ',' + this.userChoice + ',' + this.answerPercents[0] + ',' + this.answerPercents[1] + ',' + this.answerPercents[2] + ','+ this.answerPercents[3] + ',' + this.answerPercents[4] + ','+ this.answerPercents[5];
			for (var i=0;i<len;i++) {
				var winindex = this.winningAnswerValues[i];
				//remember that the answer values are 1 based index so subtract 1
				winmatrix[winindex-1]=1;
			//	delimstr = delimstr + this.oid + ',' + epoch + ',' + dd + ',' + dt + ',"' + this.question + '","' + this.contestant+'",'+this.winner+','+this.amountWon+','+this.userChoice+','+ this.numVoters + ',' + this.answerPercents[0] + ',' + this.answerPercents[1] + ',' + this.answerPercents[2] + ','+ this.answerPercents[3] + ',' + this.answerPercents[4] + ','+ this.answerPercents[5] + ',' + this.winningAnswerValues[i];
				//delimstr = delimstr +'\n';
				
			}
			for (var i=0;i<winmatrix.length;i++) {
				delimstr = delimstr +',' + winmatrix[i];
			}
			delimstr = delimstr +'\n';
		}
		catch(x) {
			alert(x);
		}
		return delimstr;
	}
}

function swakFindOTSWinPct(results) {
	if (results == null) return -1;
	var windex = -1;
	var low = 100.0;
	try {
		for (var i=0;i<results.length;i++){
			if (results[i] < low) {
				low = results[i];
				windex = i;
			}
		}
	}
	catch (x) {
		swakSetException(x);
		windex = -1;
	}
	return windex;
}

function swakGetOTSWinIndex_ORG(results,winners,rows) {
	if (true) return;
	if (results == null || winners == null) return null;
	var win = null;
	var windex = new Array();
	for (var i=0;i<winners.length;i++) {
		win = winners[i];
		try{
		for (var j=0;j<results.length;j++) {
			if (results[j].id == win) {
				windex.push(j);
				if (rows != null) {
					rows[j].className = 'swakOTSRowWinner';
				}
			}
			else {
				if (rows != null & rows[j].className != 'swakOTSRowWinner') {
					var cells = rows[j].getElementsByTagName("td");
					//tag cells as loser, don't tag first td as that is the avatar spot
					for (var cc=1;cc<cells.length;cc++) {
						cells[cc].className = 'swakOTSTDLoser';
					}
					//fyi: use this tag the entire row
					//rows[j].className = 'swakOTSRowLoser';
				}
			}
		}
	}catch(x) {alert(x);}
	}
	return windex;
}



function swakGetOTSWinIndex_ORGX(results,winners,rows,otsaccum) {
	if (results == null || winners == null || rows == null) return null;
	var storestats =  SS_getPreference(SMACK_STAT_COLLECT);
	var windex = new Array();
	var rlen = results.length;
	//magic numbers suck, but there should not be more than 6 ots ans, but I've heard of 7
	if (rlen >10) {
		rlen = 10//for now limit to 10;
	}
	try {
		if (false && winners.length == 1) {
			win = winners[0];
			for (var jj=0;jj<rlen;jj++) {
				if (results[jj].id == winners[0]) {
					windex.push(jj);
					if (rows != null && rows[jj] != null) {
						var wrow = rows[jj];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					if (rows != null && rows[jj] != null) {
						var lrow = rows[jj];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								if (cells[cc] != null) {
									cells[cc].className = 'swakOTSTDLoser';
								}
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for jj
		}
		else {
			var winids = winners.join();
			for (var k=0;k<rlen;k++) {
				var wre = new RegExp(results[k].id,"ig");
				var wf = wre.test(winids);
				if (wf == true) {
					var oid = swaksmack.SS_JF.getLOID();
			///		winningAnswerNum=k+1;
					var oldoid = SS_prefs.getCharPref(SMACK_INT_OTSRESULTOID);
					if (oid != oldoid || oldoid == "") {
						otsaccum[k] = otsaccum[k]+1;
						var key = 'tally'+k;
						otsaccum[key] = otsaccum[key]+1;
						if (storestats == true) {
							swakstats.incrementOTSWinner(k+1);
						}
					//	SS_prefs.setCharPref(SMACK_INT_OTSRESULTOID, oid);
					}
					windex.push(k);
					if (rows != null && rows[k] != null) {
						var wrow = rows[k];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					otsaccum[k] = 0;
					if (rows != null && rows[k] != null) {
						var lrow = rows[k];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//safety abort
							if (cells.length >30) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								cells[cc].className = 'swakOTSTDLoser';
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for k
		}
	}
	catch(x) {
		swakAddException(x);
		//alert(x);
		windex = null;
	}

	return windex;
}


function swakGetOTSWinIndex(otsresults,rows,otsaccum) {
	if (otsresults == null || otsresults.answers == null || otsresults.winningAnswers == null || rows == null) return null;
	var storestats =  SS_getPreference(SMACK_STAT_COLLECT);
	var results = otsresults.answers;
	var winners = otsresults.winningAnswers;
	var windex = new Array();
	var rlen = results.length;
	//magic numbers suck, but there should not be more than 6 ots ans, but I've heard of 7
	if (rlen >10) {
		rlen = 10//for now limit to 10;
	}
	try {
		if (false && winners.length == 1) {
			win = winners[0];
			for (var jj=0;jj<rlen;jj++) {
				if (results[jj].id == winners[0]) {
					windex.push(jj);
					if (rows != null && rows[jj] != null) {
						var wrow = rows[jj];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					if (rows != null && rows[jj] != null) {
						var lrow = rows[jj];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								if (cells[cc] != null) {
									cells[cc].className = 'swakOTSTDLoser';
								}
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for jj
		}
		else {
			var winids = winners.join();
			for (var k=0;k<rlen;k++) {
				var wre = new RegExp(results[k].id,"ig");
				var wf = wre.test(winids);
				if (wf == true) {
					var oid = swaksmack.SS_JF.getLOID();
					var base1 = k+1;
					otsresults.addWinningAnswer(base1);
					otsresults.winningAnswerNum=base1;
					var oldoid = SS_prefs.getCharPref(SMACK_INT_OTSRESULTOID);
					if (oid != oldoid || oldoid == "") {
						otsaccum[k] = otsaccum[k]+1;
						var key = 'tally'+k;
						otsaccum[key] = otsaccum[key]+1;
						if (storestats == true) {
							swakstats.incrementOTSWinner(base1);
						}
						//SS_prefs.setCharPref(SMACK_INT_OTSRESULTOID, oid);
					}
					windex.push(k);
					if (rows != null && rows[k] != null) {
						var wrow = rows[k];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					otsaccum[k] = 0;
					if (rows != null && rows[k] != null) {
						var lrow = rows[k];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//safety abort
							if (cells.length >30) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								cells[cc].className = 'swakOTSTDLoser';
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for k
		}
	}
	catch(x) {
		swakAddException(x);
		//alert(x);
		windex = null;
	}

	return windex;
}

function swakGetOTSWinIndex(otsresults,rows,otsaccum,updatePrefStore,enhancedDisplay) {
	if (otsresults == null || otsresults.answers == null || otsresults.winningAnswers == null || rows == null) return null;
	if (enhancedDisplay == null) enhancedDisplay = true;
	var storestats =  SS_getPreference(SMACK_STAT_COLLECT);
	var results = otsresults.answers;
	var winners = otsresults.winningAnswers;
	var cansid = otsresults.contestantAnswerId;
	var windex = new Array();
	var rlen = results.length;
	var contestantAnsFound = false;
	//magic numbers suck, but there should not be more than 6 ots ans, but I've heard of 7
	if (rlen >10) {
		rlen = 10//for now limit to 10;
	}
	try {
			var winids = winners.join();
			for (var k=0;k<rlen;k++) {
				var wre = new RegExp(results[k].id,"ig");
				if (contestantAnsFound == false && cansid != null) {
					try {
						contestantAnsFound = wre.test(cansid);
						if (contestantAnsFound == true) {
							otsresults.contestantChoice = k+1;
						}
					}
					catch(y) {
						swakAddException(y);
					}
				}
				wre.lastIndex = 0;
				var wf = wre.test(winids);
				if (wf == true) {
					var base1 = k+1;
					otsresults.addWinningAnswer(base1);
					otsresults.winningAnswerNum=base1;
					if (updatePrefStore) {
						otsaccum[k] = otsaccum[k]+1;
						var key = 'tally'+k;
						otsaccum[key] = otsaccum[key]+1;
						if (storestats == true) {
							swakstats.incrementOTSWinner(base1);
						}
						//SS_prefs.setCharPref(SMACK_INT_OTSRESULTOID, oid);
					}
					windex.push(k);
					if (enhancedDisplay && (rows != null && rows[k] != null)) {
						var wrow = rows[k];
						wrow.className = 'swakOTSRowWinner';
					}
				}
				else {
					otsaccum[k] = 0;
					if (enhancedDisplay && (rows != null && rows[k] != null)) {
						var lrow = rows[k];
						if (lrow.className != 'swakOTSRowWinner') {
							var cells = lrow.getElementsByTagName("td");
							if (cells == null) continue;
							//safety abort
							if (cells.length >30) continue;
							//tag cells as loser, don't tag first td as that is the avatar spot
							for (var cc=1;cc<cells.length;cc++) {
								cells[cc].className = 'swakOTSTDLoser';
							}
							//fyi: use this tag the entire row
							//rows[j].className = 'swakOTSRowLoser';
						}
					}
				}
			} //end for k
		
	}
	catch(x) {
		swakAddException(x);
		//alert(x);
		windex = null;
	}

	return windex;
}

	function swakCalcCurrentNumVotersStats(pSampleLen) {
		if (swakOTSNumVotersResults == null || swakOTSNumVotersResults.length == 0) {
			swakstats.numOTSVotersAvg = 0;
			swakstats.numOTSVotersStdev = 0;
			swakstats.numOTSVotersSample = 0;
		}
		if (pSampleLen == null) pSampleLen = swakOTSNumVotersResults.length;
		try {
			var avg = 0;
			var varianceAccum = 0;
			var count = 0;
			var previousAvg = 0;
			
			var sampleLen = swakOTSNumVotersResults.length;
			 if (sampleLen > pSampleLen) {
			 	sampleLen = pSampleLen;
			}
			if (sampleLen < 1) {
				swakstats.numOTSVotersAvg = 0;
				swakstats.numOTSVotersStdev = 0;
				swakstats.numOTSVotersSample = 0;
				return;
			}
			var previousValue = -1;
			var trendCount = 0;
		//	for (var i=0;i<swakOTSNumVotersResults.length;i++) {
		  //var len = sampleLen-1;
			for (var i=0;i<sampleLen;i++) {		
				if (swakOTSNumVotersResults[i] == -1 || swakOTSNumVotersResults[i] == null) {
					continue;
				}
				if (i==0) {
					avg = swakOTSNumVotersResults[i];
					previousValue = swakOTSNumVotersResults[i];
					varianceAccum = 0;
					count++;
					continue;
				}
				previousAvg = avg;
				count++;
				avg = avg + (swakOTSNumVotersResults[i] - avg) / count;
						
				varianceAccum = varianceAccum + ((swakOTSNumVotersResults[i] - previousAvg) * (swakOTSNumVotersResults[i]-avg));
				//alert(swakOTSNumVotersResults[i] + ' (current) calc stats (previous) ' + previousValue);
				if (swakOTSNumVotersResults[i] <= previousValue)
				{
					//the previous value is higher than the older one  --> trending up
					trendCount++;
				}
				else
				{
					trendCount--;
				}
				previousValue = swakOTSNumVotersResults[i];
				//alert('sample used ' + swakOTSNumVotersResults[i]);
			}
			swakstats.numOTSVotersAvg = avg;
			var variance = 0;
			if (count > 1) {
				variance = varianceAccum/(count-1);
			}
			var tstd = Math.sqrt(variance);
			swakstats.numOTSVotersStdev = Math.round(tstd*100)/100;
			swakstats.numOTSVotersSample = count;
			swakstats.OTSTrend = 0;
			var countt = count -1;
			if (trendCount >= countt)
			{
				//alert(trendCount + ' trend count, pos trend sample compare size ' + countt);
				swakstats.OTSTrend = 1;
			}
			else if(trendCount <= -countt)
			{
				//alert(trendCount + ' trend count, neg trend sample compare size ' + countt);
				swakstats.OTSTrend = -1;
			}
			
		//	alert(sampleLen + ' (samplelen) q avg ' + swakstats.numOTSVotersAvg + ' q stdev ' + swakstats.numOTSVotersStdev + ' q sample ' + swakstats.numOTSVotersSample);
		}
		catch(x) {
			swakstats.numOTSVotersAvg = 0;
			swakstats.numOTSVotersStdev = 0;
			swakstats.numOTSVotersSample = 0;
			swakstats.OTSTrend = 0;
		}
	}

	
	var pChgTol = 0.15;  //15%
	var swakPrevEst = 0;
	var swakESTTime1 = 2; //2am
	var swakESTTime2 = 7; //7am
	function swakIsHighQVotersEstimate(estimatedVoters,avgNumVoters,voterStdDev,lowPot,trend) {
		if (swakOTSNumVotersResults == null || swakOTSNumVotersResults.length == 0) {
			return 0;
		}
		if (avgNumVoters == null || avgNumVoters ==0 || voterStdDev == null || voterStdDev == 0) {
			return 0;
		}
		if (lowPot == null) {
			lowPot = false;
		}
		if (trend == null) {
			trend = 0;
		}
		 var tooclosetocall = false;
		 var favorlow = false;
		 var stdevavgratio = voterStdDev / avgNumVoters;
		 var highest = avgNumVoters + voterStdDev;
		 var prevCurPChg = 0;
		 try {
				var etemp = estimatedVoters +voterStdDev;
				if (swakPrevEst !=0 && etemp >= avgNumVoters) {
					
					var ptemp = swakPrevEst + voterStdDev;
					var esthour = swakESTGetHour();
				//	esthour = 4;
					if (esthour >= swakESTTime1 && esthour <= swakESTTime2)
					{

						ptemp = swakPrevEst + (voterStdDev *1.5);
						//alert('est night found ptemp ' + ptemp);
					}
					if (lowPot || trend == -1) {
						ptemp = ptemp *1.25;
					}
					else if (trend == 1){
						ptemp = ptemp*.9;
					}
					
					var pdiff = Math.abs(avgNumVoters - ptemp);
					
					var eststddiff = estimatedVoters - voterStdDev;
					var edtemp =  Math.abs(eststddiff - avgNumVoters);
					
					if (pdiff < edtemp ) {
						favorlow = true;
					}
					
					if (!favorlow && trend <=0 && avgNumVoters > swakPrevEst && estimatedVoters > avgNumVoters)
					{
						
						//way still be pretty close
						var preDiff = avgNumVoters - swakPrevEst;
						var curDiff = estimatedVoters - avgNumVoters;
						prevCurPChg = Math.abs((curDiff-preDiff)/preDiff);
						//alert(preDiff + ' (preDiff) still close, trending -1, curDiff ' + curDiff);
						if (prevCurPChg < pChgTol) {
							favorlow = true;
						}
						
						
					}
			
				//	alert('avgNumVoters: ' + avgNumVoters + '   pre est voters: ' + swakPrevEst + ' estimatedVoters: ' + estimatedVoters + ' voterStdDev: ' + voterStdDev+ ' pdiff: ' + pdiff + ' edtemp: ' + edtemp+  '  favorlow ' + favorlow);
				}

				var fudge1 = avgNumVoters + voterStdDev/2;
				
				var atemp = avgNumVoters*1.6; //clip at 60% increase over avg
				//alert('etemp ' + etemp + ' atemp ' + atemp + ' fudge1 ' + fudge1 + '  avg num voters: ' + avgNumVoters);
				if (etemp < fudge1) {
					swakPrevEst = estimatedVoters;
					return -1;
				}
				else if (etemp > atemp || favorlow) {
					swakPrevEst = 0;
					return 1;
				}
				else {
					swakPrevEst = 0;
					//alert(estimatedVoters + " (est) good q estimate, avg: " + avgNumVoters + ' stdev: ' + voterStdDev);
					return 0;
				}

	}
	catch(x) {alert(x);}
		return 0;
	}
	
	
		function swakIsHighQVotersEstimate0308(estimatedVoters,avgNumVoters,voterStdDev) {
		if (swakOTSNumVotersResults == null || swakOTSNumVotersResults.length == 0) {
			return 0;
		}
		if (avgNumVoters == null || avgNumVoters ==0 || voterStdDev == null || voterStdDev == 0) {
			return 0;
		}
		 var tooclosetocall = false;
		 var stdevavgratio = voterStdDev / avgNumVoters;
		 try {
				var etemp = estimatedVoters +voterStdDev;
				if (swakPrevEst !=0 && estimatedVoters > avgNumVoters) {
					
					var ptemp = swakPrevEst + voterStdDev;
					var pdiff = Math.abs(avgNumVoters - ptemp);
					
					var eststddiff = estimatedVoters - voterStdDev;
					var edtemp =  Math.abs(eststddiff - avgNumVoters);
					var pchg = Math.abs((pdiff - edtemp)/pdiff);
					if (pchg > 0.20 && stdevavgratio > 0.1) {
						tooclosetocall = true;
					}
					alert('avgNumVoters: ' + avgNumVoters + '   pre est voters: ' + swakPrevEst + ' estimatedVoters: ' + estimatedVoters + ' voterStdDev: ' + voterStdDev+ ' pdiff: ' + pdiff + ' edtemp: ' + edtemp+  '  pchg ' + pchg + '  ' + tooclosetocall);
				}

				var fudge1 = avgNumVoters + voterStdDev/2;
				
				var atemp = avgNumVoters*1.6; //clip at 60% increase over avg
			//	alert('etemp ' + etemp + ' atemp ' + atemp + ' fudge1 ' + fudge1);
				if (etemp < fudge1) {
					swakPrevEst = estimatedVoters;
					return -1;
				}
				else if (etemp > atemp || tooclosetocall) {
					swakPrevEst = 0;
					return 1;
				}
				else {
					swakPrevEst = 0;
					//alert(estimatedVoters + " (est) good q estimate, avg: " + avgNumVoters + ' stdev: ' + voterStdDev);
					return 0;
				}

	}
	catch(x) {alert(x);}
		return 0;
	}